<template>
  <div>
    <el-form
      label-width="100px"
      class="demo-company"
    >
      <el-form-item label="公司名称" prop="companyName" class="input">
        <el-input v-model="companyName" placeholder="请填写您的公司全称" />
        <i class="iconfont icon-company icon" />
        <div class="box">
          <div v-show="nameState" class="error-msg">{{ hintName }}</div>
        </div>
      </el-form-item>
      <el-form-item label="手机号码" prop="userPhone" class="input">
        <el-input v-model="userPhone" placeholder="请输入手机号码" />
        <i class="iconfont icon-phone icon" />
        <div class="box">
          <div v-show="numState" class="error-msg">{{ hintNum }}</div>
        </div>
      </el-form-item>
    </el-form>
    <button class="last" @click="last">上一步</button>
    <button class="business" @click="next">下一步</button>
  </div>
</template>
<script>
import { phoneReg, SymbolReg } from '@/js/regex'
import { checkCompanyName } from '@/api/register'
import { step } from '@/api/register/company'
export default {
  name: 'SecondCompanyRegister',
  props: {
    codeForm: {
      required: true,
      type: Object
    }
  },
  data() {
    return {
      companyName: undefined,
      userPhone: undefined,
      nameState: false,
      numState: false,
      hintName: '',
      hintNum: ''
    }
  },
  methods: {
    async next() {
      if (!this.companyName) {
        this.hintName = '请先填写公司名称'
        this.nameState = true
        return
      }
      if (SymbolReg.test(this.companyName)) {
        this.hintName = '公司名称不能包含特殊字符'
        this.nameState = true
        return
      }
      const res = await checkCompanyName(this.companyName)
      if (!res.data) {
        this.hintName = `公司 "${this.companyName}" 已被注册`
        this.nameState = true
        return
      }
      if (!this.userPhone) {
        this.nameState = false
        this.numState = true
        this.hintNum = '请先填写手机号码'
        return
      }
      if (!phoneReg.test(this.userPhone)) {
        this.nameState = false
        this.numState = true
        this.hintNum = '手机号码格式不正确'
        return
      }
      const { companyName, userPhone } = this
      const { userName, uuid } = this.codeForm
      step({ uuid }, { userName, companyName, userPhone }).then(res => {
        if (res.data) {
          this.$emit('setPasswordForm', {
            uuid: res.message,
            userName
          })
          this.$emit('next')
        }
      })
    },
    last() {
      this.$emit('last')
    }
  }
}
</script>
<style scoped lang="scss">
.registered {
  padding-top: 88px;
  padding-bottom: 87px;
  background: #f5f5f5;
  .registered-content {
    background: #ffffff;
    .contents {
      display: flex;
      justify-content: space-between;
      height: 600px;
      .left {
        padding-top: 71px;
        margin-left: 90px;
        .icon{
          position: absolute;
          left: 14px;
          font-size: 22px;
          top: 2px;
        }
        .box{
          height: 28px;
          line-height: 28px;
        }
        .error-msg{
          text-align: left;
          padding-left: 0px;
          color: #f56c6c;
        }
        .business{
          width: 400px;
          height: 42px;
          background: #FF3B30;
          color: #ffffff;
          text-align: center;
          line-height: 42px;
          cursor: pointer;
          margin-left: 100px;
          border-radius: 3px;
          font-size: 18px;
        }
        .last{
          width: 400px;
          height: 42px;
          background: #ffffff;
          margin-bottom: 15px;
          border: 1px solid #C7C7CC;
          color: #A7A7AA;
          text-align: center;
          line-height: 42px;
          cursor: pointer;
          margin-left: 100px;
          border-radius: 3px;
          font-size: 18px;
        }
        .check{
          margin-top: 18px;
          margin-left: 100px;
          font-size: 12px;
          .clause{
            color: #FF3B30;
          }
        }
      }
      .right {
        .registered-img{
          padding: 114px 40px 0 0;
        }
      }
    }
  }
  .input{
    margin-bottom: 0;
  }
  /deep/ .registered-content{
    .el-input{
      width: 400px;
      height: 42px;
      .el-input__inner{
        height: 42px;
        padding-left: 47px;
      }
    }
  }
}
</style>
